/**
 * 
 */
package org.t2.game.server.module.recharge.api;

import java.time.LocalDateTime;

import org.gaming.service.advice.ResponseResult;
import org.gaming.service.asserts.Asserts;
import org.gaming.tool.GsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.game.server.foundation.player.PlayerActorPool;
import org.t2.game.server.module.recharge.constant.RechargeStatus;
import org.t2.game.server.module.recharge.message.RechargeInternalMessage;
import org.t2.game.server.module.recharge.model.RechargeOrder;
import org.t2.game.server.module.recharge.service.RechargeOrderService;
import org.t2.game.server.module.tips.GameCause;
import org.t2.game.server.module.tips.TipsCode;

import sh.t2.share.apiutil.GameApi;
import sh.t2.share.module.recharge.RechargeApiProtocol.RechargeCallServerRequest;
import sh.t2.share.module.recharge.RechargeApiProtocol.RechargeCallServerResponse;
import sh.t2.share.module.recharge.RechargeApiProtocol.RechargeCallVirtualRequest;

/**
 * 
 */
@Controller
public class RechargeApi {
	private static Logger logger = LoggerFactory.getLogger(RechargeApi.class);

	@Autowired
	private RechargeOrderService rechargeOrderService;

	/**
	 * http://127.0.0.1:20004/rechargeApi/rechargeCallback?orderId=400000022&payMoney=100&test=true&backstageOrderId=100000010
	 * 充值回调
	 * @return
	 */
	@RequestMapping(value = GameApi.rechargeCallback)
	@ResponseBody
	public RechargeCallServerResponse rechargeCallback(@RequestBody RechargeCallServerRequest request) {
		logger.info("收到后台充值回调：{}", GsonUtil.toJson(request));
		
		synchronized (("RECHARGE" + request.orderId).intern()) {
			RechargeOrder rechargeOrder = null;
			try {
				rechargeOrder = rechargeOrderService.getEntity(request.orderId);

				Asserts.isTrue(rechargeOrder.getStatus() != RechargeStatus.SUCCESS.getType(), TipsCode.RECHARGE_GIVEN);

				rechargeOrder.setBackstageOrderId(request.backstageOrderId);
				rechargeOrder.setTest(request.test);
				rechargeOrder.setPayMoney(request.payMoney);
				rechargeOrder.setGiveTime(LocalDateTime.now());
				rechargeOrder.setStatus(RechargeStatus.SUCCESS.getType());

				PlayerActorPool.tell(RechargeInternalMessage.of(rechargeOrder));

				return new RechargeCallServerResponse();
			} catch (Exception e) {
				logger.error(String.format("充值订单%s处理异常", request.orderId), e);
				Asserts.isTrue(false, TipsCode.RECHARGE_EXCEPTION, e.getMessage());
				return new RechargeCallServerResponse();
			} finally {
				if (rechargeOrder != null) {
					rechargeOrderService.update(rechargeOrder);
				}
			}
		}
	}

	/**
	 * 虚拟充值
	 * 
	 * @param playerId
	 * @param productId
	 * @return
	 */
	@RequestMapping(value = GameApi.virtualRecharge)
	@ResponseBody
	public ResponseResult<String> virtualRecharge(@RequestBody RechargeCallVirtualRequest request) {
		logger.info("收到向玩家{}发放虚拟充值{}", request.playerId, request.productId);
		rechargeOrderService.checkOrdering(request.playerId, request.productId);
		RechargeOrder rechargeOrder = rechargeOrderService.createOrder(request.playerId, request.productId, true, GameCause.RECHARGE_VIRTUAL);
		PlayerActorPool.tell(RechargeInternalMessage.of(rechargeOrder));
		return new ResponseResult<>();
	}
}
